package com.yx.ecglineview.customview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.TreeMap;

/**
 * Created : Yx
 * Time : 2018/11/8 17:08
 * Describe :折线图
 */
public class BrokenLineView extends View {
    private Context context;

    private Paint paint1, paint2;//画折线,低压，高压
    private Paint dashedPaint;//画虚线
    private Paint paintPoint;//画点
    private Paint paintText;//画文字

    //原点
    private int mXPoint = 100;//原点的X轴坐标
    private int mYPoint = 350;//原点的Y轴坐标
    //每隔多少dp画刻度
    private int mXScale = 45;//X轴的刻度长度
    private int mYScale = 50;//Y轴的刻度长度
    //刻度线长度
    public int mXLength = 300;//X轴的长度
    private int mYLength = 300;//Y轴的长度

    private float mRange[] = new float[]{60f, 80f, 100f, 120f, 140f, 160f};//画横线、虚线数据
    private String[] mXLabel = new String[]{"07/11", "07/12", "7/13", "7/14", "7/15", "7/16", "7/17"};//X轴上的刻度
    private String[] mYLabel = new String[]{"60", "80", "100", "120", "140", "160"};//Y轴上的刻度
    private String[] mData = new String[]{"60", "80", "100", "120", " 140", "100", "75.5","90"};//需要展示的数据
    private String mTitle = "自定义折线图";

    private float maxValue;//传入数据的最大值
    private int dataNum;//数据总数


    public BrokenLineView(Context context) {
        super(context);
        this.context = context;
    }

    public BrokenLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public BrokenLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BrokenLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }

    /**
     * 设置数据
     *
     * @param dataList
     */
    public void setDatas(String[] dataList) {
        //postInvalidate();//界面刷新
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();//初始画笔
        drawAboutYs(canvas);//设置虚线
        initLine(canvas);//画X.Y坐标线及刻度
        inItBrokenLinOne(canvas);//折线图
    }

    /**
     * 初始画笔
     */
    private void initPaint() {
        paint1 = new Paint();
        paint1.setStyle(Paint.Style.STROKE);//只绘制图形轮廓(描边)
        paint1.setStrokeWidth(2);//设置线宽
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(Color.DKGRAY);//深灰
        paint1.setTextSize(12);

        // 虚线
        dashedPaint = new Paint();
        dashedPaint.setAntiAlias(true);//抗锯齿
        dashedPaint.setFilterBitmap(true);//对位图进行滤波处理
        dashedPaint.setStyle(Paint.Style.STROKE);//只绘制图形轮廓(描边)
        dashedPaint.setStrokeWidth(4);//设置线宽
        /**
         * public DashPathEffect(float intervals[], float phase)
         * intervals[]：表示组成虚线的各个线段的长度；整条虚线就是由intervals[]中这些基本线段循环组成的。
         * phase：开始绘制的偏移值
         * 比如，我们定义new float[] {20,10}；那这个虚线段就是由两段线段组成的，第一个可见的线段长为20，每二个线段不可见，长度为10；
         */
        PathEffect effect = new DashPathEffect(new float[]{5, 5, 5, 5}, 5);
        dashedPaint.setPathEffect(effect);//设置绘制路径的效果，如点画线等

        //文字
        paintText = new Paint();
        paintText.setStyle(Paint.Style.STROKE);
        paintText.setAntiAlias(true);//去锯齿
        paintText.setColor(Color.DKGRAY);//深灰
        paintText.setTextSize(12);

        //点
        paintPoint = new Paint();
        paintPoint.setStyle(Paint.Style.STROKE);
        paintPoint.setAntiAlias(true);//去锯齿
        paintPoint.setStyle(Paint.Style.FILL);//实心
    }

    /**
     * 根据点数，画横线
     *
     * @param canvas
     */
    private void drawAboutYs(Canvas canvas) {
        // float y坐标 Integer y坐标对应的横线的颜色
        TreeMap<Float, Integer> yCoords = new TreeMap<>();
        yCoords.put(mRange[0], Color.parseColor("#FF0000"));//红色偏低
        yCoords.put(mRange[1], Color.parseColor("#C8E1A8"));//绿色正常
        yCoords.put(mRange[2], Color.parseColor("#FBAD5F"));//橘色偏高
        yCoords.put(mRange[3], Color.parseColor("#FBAD5F"));//橘色偏高
        yCoords.put(mRange[4], Color.parseColor("#FBAD5F"));//橘色偏高
        yCoords.put(mRange[5], Color.parseColor("#FBAD5F"));//橘色偏高


        for (int i = 0; i < yCoords.size(); i++) {
            // y坐标上的线的颜色
            dashedPaint.setColor(yCoords.get(mRange[i]));
            float ys = mYPoint - i * 50;
            Path path = new Path();
            // 画线
            path.reset();
            path.moveTo(getPaddingLeft(), ys);//起始点坐标
            path.lineTo(getWidth(), ys);//终点坐标
            canvas.drawPath(path, dashedPaint);
            canvas.drawText(mRange[i] + "", getPaddingLeft() + 20, ys - 10, paintText);
        }

    }

    /**
     * 画X.Y轴，及刻度
     *
     * @param canvas
     */
    private void initLine(Canvas canvas) {
        //根据数据的数量计算X轴的刻度长度
        calculateXScale();


        //设置Y轴
        canvas.drawLine(mXPoint, mYPoint, mXPoint, mYPoint - mYLength, paint1);
        //Y箭头
        canvas.drawLine(mXPoint, mYPoint - mYLength, mXPoint - 3, mYPoint - mYLength + 6, paint1);
        canvas.drawLine(mXPoint, mYPoint - mYLength, mXPoint + 3, mYPoint - mYLength + 6, paint1);


        //设置X轴
        canvas.drawLine(mXPoint, mYPoint, mXPoint + mXLength, mYPoint, paint1);//画轴线
        //X箭头
        canvas.drawLine(mXPoint + mXLength, mYPoint, mXPoint + mXLength - 6, mYPoint - 3, paint1);
        canvas.drawLine(mXPoint + mXLength, mYPoint, mXPoint + mXLength - 6, mYPoint + 3, paint1);

        //--X-Y刻度
        //设置Y轴上的刻度显示(如果没有for循环,将只会是一个Y轴的直线)
        for (int i = 0; i * mYScale < mYLength; i++) {
            //画刻度线
            canvas.drawLine(mXPoint, mYPoint - i * mYScale, mXPoint + 5, mYPoint - i * mYScale, paint1);//刻度
            try {
                //画轴上的刻度值
                canvas.drawText(mYLabel[i], mXPoint - 24, mYPoint - i * mYScale + 5, paintText);//刻度
            } catch (Exception e) {
            }
        }

        for (int i = 0; i * mXScale < mXLength; i++) {
            //画刻度
            canvas.drawLine(mXPoint + i * mXScale, mYPoint, mXPoint + i * mXScale, mYPoint - 5, paint1);
            try {
                //画刻度值
                canvas.drawText(mXLabel[i], mXPoint + i * mXScale - 15, mYPoint + 20, paintText);
            } catch (Exception e) {
            }
        }


    }

    //根据数据的数量计算横坐标刻度
    private Integer calculateXScale() {
        mXLength = getWidth() - 200;
        mXScale = mXLength / (mData.length - 1);
        return mXScale;
    }

    /**
     * 低压 折线1
     *
     * @param canvas
     */
    private void inItBrokenLinOne(Canvas canvas) {
        for (int i = 0; i * mXScale < mXLength; i++) {
            float ys = mYPoint - (Float.parseFloat(mData[i]) - 60) * 2.5f;//当前折线点坐标
            //数据值(两点确定一条直线)
            if (i > 0) {  //大于0开始画线
                //画折线
                float yr = mYPoint - (Float.parseFloat(mData[i - 1]) - 60) * 2.5f;//上一折线点坐标
                canvas.drawLine(mXPoint + (i - 1) * mXScale, yr, mXPoint + i * mXScale, ys, paint1);
            }

            //画转折点上方的数据值
            canvas.drawText(mData[i], mXPoint + i * mXScale, ys - 15, paintText);

            if (Float.parseFloat(mData[i]) <= mRange[0]) {//小于等于60刻度 红色
                paintPoint.setColor(Color.parseColor("#FF0000"));
            } else if (Float.parseFloat(mData[i]) >= mRange[2]) {//大于等于100刻度 橙色
                paintPoint.setColor(Color.parseColor("#FBAD5F"));
            } else {//大于60小于100的为 绿色
                paintPoint.setColor(Color.parseColor("#C8E1A8"));
            }
            //设置圆点为实心的
//            paint.setStyle(Paint.Style.FILL);
            //画两线之间的转折点
            canvas.drawCircle(mXPoint + i * mXScale, ys, 6, paintPoint);


        }

        //设置标题的内容,位置,字体大小
        paintText.setTextSize(16);
        canvas.drawText(mTitle, 200, 20, paintText);
    }


}
